package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.BankListAdapter;
import com.store.app.bean.BankInfo;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */
//选择银行卡页面
public class ChooseBankActivity extends BaseActivity implements IOnHttpListener,View.OnClickListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private AutoListView lv_bank;
    private NetRequest netRequest;
    private int pageNum = 1;
    private List<BankInfo> bankInfoList;
    private BankListAdapter bankListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_bank);
        netRequest = new NetRequest(this);
        initData();
        initView();

    }

    public void initData() {
        bankInfoList = new ArrayList<>();
        bankListAdapter = new BankListAdapter(this,bankInfoList);
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId==1){
            try {
                lv_bank.onLoadComplete();
                JSONObject jsonObject = new JSONObject(result);
                JSONArray  jsonArray  = jsonObject.getJSONArray("list");
                List<BankInfo> tempList = new ArrayList<>();
                for (int i =0;i<jsonArray.length();i++){
                    BankInfo info = new BankInfo();
                    info.setBank_code(jsonArray.getJSONObject(i).getString("bank_code"));
                    info.setBank_name(jsonArray.getJSONObject(i).getString("bank_name"));
                    tempList.add(info);
                }


                if (tempList == null) {
                    return;
                }
                lv_bank.setResultSize(tempList.size());
                bankInfoList.addAll(tempList);
                bankListAdapter.notifyDataSetChanged();
                pageNum += 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(requestId == 2){
            try {
                lv_bank.onRefreshComplete();
                JSONObject jsonObject = new JSONObject(result);
                JSONArray  jsonArray  = jsonObject.getJSONArray("list");
                List<BankInfo> tempList = new ArrayList<>();
                for (int i =0;i<jsonArray.length();i++){
                    BankInfo info = new BankInfo();
                    info.setBank_code(jsonArray.getJSONObject(i).getString("bank_code"));
                    info.setBank_name(jsonArray.getJSONObject(i).getString("bank_name"));
                    tempList.add(info);
                }


                if (tempList == null) {
                    return;
                }
                lv_bank.setResultSize(tempList.size());
                bankInfoList.clear();
                bankInfoList.addAll(tempList);
                bankListAdapter.notifyDataSetChanged();
                pageNum += 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        if (requestId == 1) {
            lv_bank.setResultSize(0);
            lv_bank.onLoadComplete();
        } else if (requestId == 2) {
            lv_bank.onRefreshComplete();
        }
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("选择银行");
        public_ll_return.setOnClickListener(this);
        lv_bank = (AutoListView) findViewById(R.id.lv_bank);
        lv_bank.setAdapter(bankListAdapter);
        lv_bank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("zyl","onItemClick position"+position);
                if (position >= 1 && position <= bankInfoList.size()) {

                    Intent data = new Intent();
                    // 设置要回传的数据
                    data.putExtra("bank_code", bankInfoList.get(position-1).getBank_code());
                    data.putExtra("bank_name", bankInfoList.get(position-1).getBank_name());
                    Log.v("zyl","bank_code:"+bankInfoList.get(position-1).getBank_code());
                    Log.v("zyl","bank_name:"+bankInfoList.get(position-1).getBank_name());
                    // 设置回传码和Intent
                    setResult(88, data);
                    finish();
                }
            }
        });
        lv_bank.setOnRefreshListener(new AutoListView.OnRefreshListener() {

            @Override
            public void onRefresh() {
                pageNum = 1;
                netRequest.requestBankList(2,pageNum+"");
            }
        });

        lv_bank.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                netRequest.requestBankList(1, pageNum+"");
            }
        });
        lv_bank.onRefresh();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.public_ll_return:
                finish();
                break;
        }
    }
}
